<template>
  <!-- ASN单表单弹窗 -->
  <div>
    <el-dialog
      :close-on-click-modal="false"
      v-model="visibleDialog"
      :title="props.title"
      width="500"
      draggable
      @close="closeDialog"
    >
      <el-form
        ref="formRef"
        :model="formData"
        :inline="true"
        label-width="100"
      >
        <h4 class="mb-[20px] mt-[0px]">收货存储地点</h4>
        <div class="search-container">
          <el-row>
            <el-col :span="16">
              <el-form-item label="存储地点" prop="storageLocationId">
                <el-select
                  style="width: 250px"
                  v-model="formData.srmPartAsnDTO.storageLocationId"
                  @change="storageLocationChange"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in storageLocations"
                    :key="item.id"
                    :label="item.storageLocationName"
                    :value="item.id"
                  />
                </el-select> </el-form-item
            ></el-col>
          </el-row>
        </div>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button
            v-if="!props.isReadonly"
            type="primary"
            :loading="loading"
            @click="handleSubmit"
            >确 定</el-button
          >
          <el-button @click="closeDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "SRMASNPARTADDORRECEIVE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);
const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

/** 弹窗显示 **/
const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    console.log("newVal", newVal);
    visibleDialog.value = newVal;
    getStorageLocations();
    if (newVal === true) {
      setTimeout(() => {
        formRef.value.clearValidate();
      }, 50);
    }
  }
);

/** formData定义表单数据 */
const formRef = ref(ElForm);
let formData = reactive({});

/** 编辑时赋值 */
watch(
  () => props.data,
  (newVal: any) => {
    console.log(props.data);
    Object.assign(formData, props.data);
    console.log(formData);
    getStorageLocations();
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
}

let storageLocations = ref([]);
const loading = ref(false);
function storageLocationChange() {
  var storageLocation = storageLocations.value.find((item) => {
    return item.id === formData.srmPartAsnDTO.storageLocationId;
  });
  formData.srmPartAsnDTO.storageLocationCode =
    storageLocation.storageLocationCode;
  formData.srmPartAsnDTO.storageLocationName =
    storageLocation.storageLocationName;
}

async function getStorageLocations() {
  const data = await request({
    url: "/wms/storagelocation/getIMList/",
    method: "get",
    params: {},
  });
  storageLocations.value = data.data;
}
/** ASN单保存提交 */
function handleSubmit() {
  console.log("11111", formData);
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      await request({
        url: "/wms/rmreceipt/receiptPM",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(`收货成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}
</script>

<style lang="scss" scoped>
h4 {
  font-weight: 400;
  color: #000;
  padding-left: 10px;
  font-size: 14px;
  line-height: 14px;
}
.formTable {
  :deep(.el-form-item) {
    margin-right: 0 !important;
  }
}
:deep(.el-dialog .el-dialog__body) {
  overflow-y: auto;
}
:deep(.search-container .el-input.el-input--default.el-input--suffix) {
  // 固定宽度
  width: 250px !important;
}
:deep(.search-container .el-form-item--default .el-form-item__label) {
  // 固定宽度
  justify-content: end;
}
</style>
